/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.parser;

import com.anhndt.audiobook.business.Service;
import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.entity.ItemPartEntity;
import com.anhndt.audiobook.utils.StringUtil;
import com.anhndt.audiobook.utils.Util;
import java.util.Vector;
import org.ksoap2.serialization.SoapObject;

/**
 *
 * @author Anhndt
 */
public class ItemParser {

    private WebServicesParser parser;
    Service service = new Service(Service.METHOD_GET_CONTENT);
    public boolean checkLoaded = true;
    public boolean showError = true;

    public ItemParser() {
        parser = new WebServicesParser(service);
    }
    
    public ItemParser(Service service) {
        this.service = service;
        parser = new WebServicesParser(service);
    }

    //Contructor for Category:

    public ItemParser(ItemEntity entity) {
        this(entity, null, null, null, false);
    }
    //Contructor for Article + articlepart

    public ItemParser(ItemEntity entity, String keyWord, String pageSize, String pageIndex, boolean showThumb) {
        this(Util.userID, null, entity.id, "" + entity.typeEntity, keyWord, pageSize, pageIndex, showThumb);
    }
    //Contructor for search

    public ItemParser(String keyWord, String pageSize, String pageIndex) {
        this(Util.userID, null, null, null, keyWord, pageSize, pageIndex, true);
    }
    //Contructor for Favorite

    public ItemParser(String id, byte contentType) {
        this(Util.userID, Util.passwd, id, "" + contentType, null, null, null, false);
    }

    public ItemParser(String id, String contentType, String keyWord, String pageSize, String pageIndex, boolean thumb) {
        this(Util.userID, Util.passwd, id, contentType, keyWord, pageSize, pageIndex, thumb);
    }

    public ItemParser(String userId, String passwd, String id, String contentType, String keyWord, String pageSize, String pageIndex, boolean thumb) {
        this();
        addProperty("u", userId);
        addProperty("pw", passwd);
        addProperty("i", id);
        addProperty("y", contentType);
        addProperty("k", keyWord);
        addProperty("p", pageIndex);
        addProperty("n", pageSize);
        if (thumb) {
            addProperty("t", "1");
        }
    }

    public void addProperty(String key, String value) {
        if (!StringUtil.isNullOrEmpty(value)) {
            service.addProperty(key, value);
        }
    }

    public void callservice() {
        try {
            parser.getResponse();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public boolean getResponse(ItemEntity parent) throws Exception {
        if (parent == null) {
            throw new NullPointerException("Item Parent or keys is not null");
        }
        SoapObject response = parser.getResponse();
//        if(parent.typeEntity == ItemEntity.TYPE_ARTICLE_PART || parent.typeEntity == ItemEntity.TYPE_ADVERTISE_IMAGE)
            System.out.println("reponse = " + response);
        ItemEntity itemEntity = null;
        if (response != null || response.getPropertyCount() != 0) {
            SoapObject item = null;
            String id, name, description, thumbnail, marked, url, contentType, other;
            int rate = 3;
            int counterChild;
            for (int i = 0; i < response.getPropertyCount(); i++) {
                item = (SoapObject) response.getProperty(i);
                //get all property
                id = parser.getProperty(item, ItemEntity.ID);
                name = parser.getProperty(item, ItemEntity.NAME);
                description = parser.getProperty(item, ItemEntity.DESCRIPTION);
                thumbnail = parser.getProperty(item, ItemEntity.THUMBNAIL);
                marked = parser.getProperty(item, ItemEntity.MARKED);

                url = parser.getProperty(item, ItemEntity.URL);
                contentType = parser.getProperty(item, ItemEntity.CONTENT_TYPE);
                other = parser.getProperty(item, ItemEntity.OTHER);
                String s = parser.getProperty(item, ItemEntity.COUNTER_CHILD);
                counterChild = Integer.parseInt(s == null ? "0" : s);
                String sRate = parser.getProperty(item, ItemEntity.RATE);
                if (!StringUtil.isNullOrEmpty(sRate)) {
                    float fRate = Float.parseFloat(sRate);
                    fRate += 0.5;
                    rate = (int) fRate;
                }
                //hard code if type is category
                if (contentType != null &&contentType.equals("" + ItemEntity.TYPE_CATEGORY)) {
                    description = "" + Math.abs(counterChild) + " ";
                    description += (counterChild > 0) ? "Audio" : "";
                }
                if (contentType != null && contentType.equals("" + ItemEntity.TYPE_ARTICLE_PART)) {
                    itemEntity = new ItemPartEntity(id, name, description, thumbnail, marked, rate, url, other, contentType, counterChild);
                    if (id.equals(Util.lastArticlePartID)) {
                        ((ItemPartEntity) itemEntity).setPlaying(true);
                    }
                } else {
                    itemEntity = new ItemEntity(id, name, description, thumbnail, marked, rate, url, other, contentType, counterChild);
                }
                itemEntity.setParent(parent);
                Vector vt = parent.getValue(contentType);
                vt.addElement(itemEntity);
//                if (validate(itemEntity)) {
//                    Vector vt = parent.getValue(contentType);
//                    vt.addElement(itemEntity);
//
//                    continue;
//                }
            }
            response = null;
            return true;
        }
//        else {
//            DialogUtil.showErrorUpdating();
//            return false;
//        }
        return false;
    }

//    boolean validate(ItemEntity entity) {
//        if ((entity.typeEntity == ItemEntity.TYPE_CATEGORY
//                || entity.typeEntity == ItemEntity.TYPE_ARTICLE
//                || entity.typeEntity == ItemEntity.TYPE_ARTICLE_PART) && entity.counterChild == 0) {
//            return false;
//        }
//        return true;
//    }
    public void release() {
        parser = null;
    }
}
